
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for browse.
	''' </summary>
	Public Partial Class browse
		Inherits BaseWebPage
		Protected CheckBox9 As System.Web.UI.WebControls.CheckBox


		Private usersFound As ArrayList = Nothing
		Private usersFoundCount As Double = 0

		Protected blankImagesCount As Integer = 0
		Protected currentPageNumber As Integer = 0
		Protected firstPageNumber As Integer = 0
		' the first page of page links
		Protected lastPageNumber As Integer = 0

		Protected searchCriteria As String = ""

		Protected countryID As Integer = -1
		Protected city As String = ""
		Protected bClearCity As Boolean = False
		'ivan change - this will show if only country filtetr should be used
		Protected bExternalPageSearch As Boolean = False
		'ivan change - this will show if the browse is started from anoder page
		Private bIsSearchCriteriaSelected As Boolean = True
		'ivan change
		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.BROWSE_PAGE;
			Dim usr As User = Me.LoggedUser

			'			if(usr == null)
			'				Common.Utils.RedirectToLoginPage(Page);

			' init page from XML
			Me.initPageTitles(Constants.BROWSE, "", Form1, Nothing, Nothing)
			Me.initAdvertisements(Form1, Nothing)

			'change by ivan - all time the user settings should be loaded when the user visits the browse page
			'			if( (usr != null) && (Request.QueryString["load"]=="default_usr") ) 
'			{
'				initBrowseCookies(usr);	
'				Response.Redirect(Constants.BROWSE);
'			}
'			else 
'			{
'				if ( (usr != null) && 
'					(Request.Cookies[Constants.cookieBrowse_Filter] == null || 
'					Request.Cookies[Constants.cookieBrowse_Filter].Value == "") ) 
'				{
'					initBrowseCookies(usr);
'				}
'				else
'				{ 				
'					if( (usr == null) && 
'						(Session[Constants.ssnBrowseFilter] == null || 
'						Session[Constants.ssnBrowseFilter].ToString() == "") )
'					{
'						ssn_InitBrowseSession();
'					}
'				}
'			}
'

			'if( usr != null )
			'	initBrowseCookies(usr);

			If (usr <> Nothing) AndAlso ((Request.Cookies(Constants.cookieBrowse_Filter) = Nothing) OrElse (Request.Cookies(Constants.cookieBrowse_Filter).Value = "")) Then
				initBrowseCookies(usr)
			Else
				If (usr = Nothing) AndAlso (Session(Constants.ssnBrowseFilter) = Nothing OrElse Session(Constants.ssnBrowseFilter).ToString() = "") Then
					ssn_InitBrowseSession()
				End If
			End If

			dlResults.RepeatColumns = Constants.MAX_LIST_RESULTS_ON_ROW

			If Not Me.IsPostBack Then
				' init age
				InitAgeDropdown()
				' init countries
				InitCountriesDropdown()
			End If

			Dim filter As New UserFilter()
			filter.orderBy = UserFilter.OrderBy.recentlyUpdated

			If (usr <> Nothing AndAlso Request.Cookies(Constants.cookieBrowse_ViewDetails) <> Nothing AndAlso Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_ViewDetails).Value) = False) OrElse (usr = Nothing AndAlso Session(Constants.ssnBrowse_ViewDetails) <> Nothing AndAlso Convert.ToBoolean(Session(Constants.ssnBrowse_ViewDetails)) = False) Then
				rbJustPhotos.Checked = True
			Else
				rbPhotosAndInfo.Checked = True
			End If

			' set filter if COUNTRY param is given (form details table - "view my profile" page, "members" page)
			If Request.Params(Constants.COUNTRY_PARAM) <> Nothing AndAlso Request.Params(Constants.COUNTRY_PARAM) <> "" Then
				countryID = Convert.ToInt32(Request.Params(Constants.COUNTRY_PARAM))
				bClearCity = True
				city = ""
				bExternalPageSearch = True
			End If

			' set filter if CITY param is given (form details table - "view my profile" page, "members" page)
			If Request.Params(Constants.CITY_PARAM) <> Nothing AndAlso Request.Params(Constants.CITY_PARAM) <> "" Then
				city = Request.Params(Constants.CITY_PARAM)
				If Request.Params(Constants.COUNTRY_PARAM) = Nothing OrElse Request.Params(Constants.COUNTRY_PARAM) = "" Then
						'if is -1 will not work 
					countryID = -2
				End If
				bExternalPageSearch = True
			End If

			If (usr <> Nothing AndAlso Request.Cookies(Constants.cookieBrowse_Filter) <> Nothing) OrElse (usr = Nothing AndAlso Session(Constants.ssnBrowseFilter) <> Nothing) Then
				If Not Me.IsPostBack Then
					'ivan change - this will load default settings for filter when the search request is from external page (ex. city / country)
					'	if(bExternalPageSearch==false) 
					'	{
					If usr <> Nothing Then
						filter = initUserFilter()
					Else
						filter = DirectCast(Session(Constants.ssnBrowseFilter), UserFilter)
					End If
					'	}

					' init photo check
					If filter.hasPhoto = True Then
						chkOnlyPeople.Checked = True
					Else
						chkOnlyPeople.Checked = False
					End If

					' init gender					
					If filter.gender = UserFilter.Gender.ufgMale Then
						rbMen.Checked = True
ElseIf filter.gender = UserFilter.Gender.ufgFemale Then
						rbWomen.Checked = True
					Else
						rbAll.Checked = True
					End If

					' init activity preference

					chkMeetPeopleDating.Checked = filter.activityPreference.dating
					chkMeetPeopleSR.Checked = filter.activityPreference.relationship
					chkMeetPeopleFriends.Checked = filter.activityPreference.friends
					chkMeetPeopleAP.Checked = filter.activityPreference.partners
					chkMeetPeoplePenpals.Checked = filter.activityPreference.penpals
					chkMeetPeopleHangingOut.Checked = filter.activityPreference.hangout

					'chkMeetPeopleAll.Checked = 
					'	!filter.activityPreference.dating &&
					'	!filter.activityPreference.relationship &&
					'	!filter.activityPreference.friends &&
					'	!filter.activityPreference.partners &&
					'	!filter.activityPreference.penpals &&
					'	!filter.activityPreference.hangout;

					If (usr <> Nothing AndAlso Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isMeetPeopleAllChecked) <> Nothing AndAlso Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isMeetPeopleAllChecked)) = True) OrElse (usr = Nothing AndAlso Session(Constants.ssnIsMeetPeopleAllChecked) <> Nothing AndAlso Convert.ToBoolean(Session(Constants.ssnIsMeetPeopleAllChecked)) = True) Then
						chkMeetPeopleAll.Checked = True
					Else
						chkMeetPeopleAll.Checked = False
					End If

					If (usr <> Nothing AndAlso chkMeetPeopleAll.Checked AndAlso Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isMeetPeopleAllChecked) <> Nothing AndAlso Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isMeetPeopleAllChecked)) = True) OrElse (usr = Nothing AndAlso chkMeetPeopleAll.Checked AndAlso Session(Constants.ssnIsMeetPeopleAllChecked) <> Nothing AndAlso Convert.ToBoolean(Session(Constants.ssnIsMeetPeopleAllChecked)) = True) Then
						chkMeetPeopleDating.Checked = True
						chkMeetPeopleSR.Checked = True
						chkMeetPeopleFriends.Checked = True
						chkMeetPeopleAP.Checked = True
						chkMeetPeoplePenpals.Checked = True
						chkMeetPeopleHangingOut.Checked = True
					End If

					' init age
					Dim ageFrom As String = filter.ageFrom.ToString()
					Dim ageTo As String = filter.ageTo.ToString()
					ddlminAge.ClearSelection()
					ddlminAge.Items.FindByText(ageFrom).Selected = True
					ddlmaxAge.ClearSelection()
					ddlmaxAge.Items.FindByText(ageTo).Selected = True

					'init status

					chkStatusSingle.Checked = filter.status.[single]
					chkStatusInRelationship.Checked = filter.status.relationship
					chkStatusOpenRelationship.Checked = filter.status.openRelationship
					chkStatusMarried.Checked = filter.status.married
					chkStatusOpenMerriage.Checked = filter.status.openMarriage
					chkStatusUnknown.Checked = filter.status.unspecified

					' Any means NO filter - which is not true if ANY status filter is set to true
					'chkStatusAny.Checked =
					'	!filter.status.single &&
					'	!filter.status.relationship &&
					'	!filter.status.openRelationship &&
					'	!filter.status.married &&
					'	!filter.status.openMarriage &&
					'	!filter.status.unspecified;

					If (usr <> Nothing AndAlso Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isStatusAnyChecked) <> Nothing AndAlso Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isStatusAnyChecked)) = True) OrElse (usr = Nothing AndAlso Session(Constants.ssnIsStatusAnyChecked) <> Nothing AndAlso Convert.ToBoolean(Session(Constants.ssnIsStatusAnyChecked)) = True) Then
						chkStatusAny.Checked = True
					Else
						chkStatusAny.Checked = False
					End If

					If (usr <> Nothing AndAlso chkStatusAny.Checked AndAlso Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isStatusAnyChecked) <> Nothing AndAlso Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isStatusAnyChecked)) = True) OrElse (usr = Nothing AndAlso chkStatusAny.Checked AndAlso Session(Constants.ssnIsStatusAnyChecked) <> Nothing AndAlso Convert.ToBoolean(Session(Constants.ssnIsStatusAnyChecked)) = True) Then
						chkStatusSingle.Checked = True
						chkStatusInRelationship.Checked = True
						chkStatusOpenRelationship.Checked = True
						chkStatusMarried.Checked = True
						chkStatusOpenMerriage.Checked = True
						chkStatusUnknown.Checked = True
					End If

					' init country
					If countryID <> -1 Then
						filter.countryID = countryID
					End If

					If filter.countryID > 0 Then
						ddlCountry.ClearSelection()
						ddlCountry.Items.FindByValue(filter.countryID.ToString()).Selected = True
					End If

					If city <> "" Then
						filter.city = city
					End If

					If bClearCity = True Then
						filter.city = Nothing
					End If

					' init city / town
					If filter.city <> Nothing AndAlso filter.city <> "" Then
						txtCity.Text = filter.city
					End If
				End If

				' init OrderBy option					
				If filter.orderBy = UserFilter.OrderBy.recentlyUpdated Then
					rbListByRecentlyUpdated.Checked = True
ElseIf filter.orderBy = UserFilter.OrderBy.lastLogin Then
					rbListByLastLogin.Checked = True
ElseIf filter.orderBy = UserFilter.OrderBy.newUsers Then
					rbListByNewest.Checked = True
				End If
			End If

			bIsSearchCriteriaSelected = IsSearchCriteriaSelected(filter)
			filter.ageMetters = True
			'ivan change - fix for age search 
			'			if (bExternalPageSearch==true) 
'			{
'				//filter.ageMetters = false; -
'				if ( usr != null )
'				{
'					//Response.Cookies[Constants.cookieBrowse_Criteria].Value = Server.UrlEncode(searchString);
'					initBrowseCookies(filter);
'				}
'				else
'				{
'					//Session[Constants.ssnBrowse_Criteria] = Server.UrlEncode(searchString);
'					ssn_InitBrowseSession(filter);
'				}
'			}
'

			If bIsSearchCriteriaSelected = True Then
				usersFound = Users.FindUsers(filter)
				usersFoundCount = usersFound.Count
			End If

			BindPagerControl(usersFoundCount)

			If bExternalPageSearch = False Then
				If bIsSearchCriteriaSelected = True Then
					If usr <> Nothing AndAlso Request.Cookies(Constants.cookieBrowse_Criteria) <> Nothing Then
						searchCriteria = Server.UrlDecode(Request.Cookies(Constants.cookieBrowse_Criteria).Value)
					End If

					If usr = Nothing AndAlso Session(Constants.ssnBrowse_Criteria) <> Nothing Then
						searchCriteria = Server.UrlDecode(Session(Constants.ssnBrowse_Criteria).ToString())
					End If
				Else
					searchCriteria = "Please choose search criteria"
				End If
			End If
			'			else 
'			{
'				if( usr!= null && Request.Cookies[Constants.cookieBrowse_Criteria] != null ) 
'					Request.Cookies[Constants.cookieBrowse_Criteria].Value = null;
'								
'				if( usr == null && Session[Constants.ssnBrowse_Criteria] != null ) 				
'					Session[Constants.ssnBrowse_Criteria] = null;
'							
'			}
'


			If Not Me.IsPostBack Then
				dgResults.VirtualItemCount = Convert.ToInt32(usersFoundCount)

				If (usr <> Nothing AndAlso Request.Cookies(Constants.cookieBrowse_ViewDetails) <> Nothing AndAlso Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_ViewDetails).Value) = False) OrElse (usr = Nothing AndAlso Session(Constants.ssnBrowse_ViewDetails) <> Nothing AndAlso Convert.ToBoolean(Session(Constants.ssnBrowse_ViewDetails)) = False) Then
					dgResults.Visible = False
					'rbJustPhotos.Checked = true;
					If usersFoundCount <> 0 Then
						dlResults.Visible = True
						BindList()
					End If
				Else
					dlResults.Visible = False
					'rbPhotosAndInfo.Checked = true;
					If usersFoundCount <> 0 Then
						dgResults.Visible = True
						BindGrid()
					End If
				End If
			End If
		End Sub

		Public Function CreateDataSourceForList() As ICollection
			Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
			Dim endIndex As Integer = startIndex + Constants.MAX_LIST_RESULTS_ON_PAGE
			Dim i As Integer
			Dim dt As New DataTable()
			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("ImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Name", GetType(String)))
			dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))
			dt.Columns.Add(New DataColumn("UserID", GetType(String)))
			dt.Columns.Add(New DataColumn("MemberURL", GetType(String)))
			dt.Columns.Add(New DataColumn("FullName", GetType(String)))

			Dim temp As UserPicture
			Dim imagesOnLastRow As Integer = 0
			Dim t As Integer = 0
			Dim counter As Integer = 0

			txtFriendsCountHidden.Text = usersFoundCount.ToString()
			endIndex = Math.Min(endIndex, Convert.ToInt32(usersFoundCount))
			t = (endIndex - startIndex)
			Math.DivRem(t, Constants.MAX_LIST_RESULTS_ON_ROW, imagesOnLastRow)
			dgPager.dgListing.StartIndex = startIndex + 1
			dgPager.dgListing.EndIndex = endIndex
			dgPagerUp.dgListing.StartIndex = startIndex + 1
			dgPagerUp.dgListing.EndIndex = endIndex

			counter = 0
			i = 0

			Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

			For Each result As User In usersFound
				If counter >= startIndex AndAlso counter < endIndex Then
					dr = dt.NewRow()
					temp = result.DefaultPicture

					If temp <> Nothing Then
						dr(0) = Pictures.GetPictureSrcFullURL(temp, True, "../")
					Else
						dr(0) = "../images/default_thumbnail.gif"
					End If
					dr(1) = getBeginning(HttpUtility.HtmlEncode(result.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					If LoggedUsersList.Contains(result.ID) Then
						dr(2) = "../images/online_status.gif"
					Else
						dr(2) = "../images/spacer.gif"
					End If
					dr(3) = result.ID
					dr(4) = "../" + Constants.MEMBERS + "?id=" + result.ID
					dr(5) = result.Name
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
			Next

			If imagesOnLastRow <> 0 Then
				blankImagesCount = Constants.MAX_LIST_RESULTS_ON_ROW - imagesOnLastRow
				i = imagesOnLastRow
				While i < Constants.MAX_LIST_RESULTS_ON_ROW
					dr = dt.NewRow()
					'dr[0] = "../images/empty_image.gif";
					dr(0) = "../images/spacer.gif"
					dr(1) = ""
					dr(2) = "../images/spacer.gif"
					dr(3) = ""
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function


		Public Sub BindList()
			dlResults.DataSource = CreateDataSourceForList()
			dlResults.DataBind()

			Dim itemsCount As Integer = dlResults.Items.Count
			Dim blankImageIndex As Integer = itemsCount - blankImagesCount
			Dim i As Integer = 0
			i = blankImageIndex
			While i < itemsCount
				(DirectCast(dlResults.Items(i).FindControl("lbName"), System.Web.UI.WebControls.LinkButton)).CssClass = "empty_image"
				(DirectCast(dlResults.Items(i).FindControl("hlPhoto"), System.Web.UI.WebControls.HyperLink)).CssClass = "empty_image"
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
		End Sub

		Protected Function CreateDataSource() As ICollection
			Dim usr As BO.User = Me.LoggedUser

			Dim dt As New DataTable()

			If usersFoundCount <= 0 Then
				dgPager.FirstPageNumber = 1
				dgPager.LastPageNumber = 1
				dgPager.MaxPageNumber = 1
				dgPager.PagesRest = 1
				dgPager.dgListing.StartIndex = 0
				dgPager.dgListing.EndIndex = 0
				dgPagerUp.FirstPageNumber = 1
				dgPagerUp.LastPageNumber = 1
				dgPagerUp.MaxPageNumber = 1
				dgPagerUp.PagesRest = 1
				dgPagerUp.dgListing.StartIndex = 0
				dgPagerUp.dgListing.EndIndex = 0
				dgResults.AllowSorting = False
			Else
				Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
				Dim endIndex As Integer = startIndex + Constants.MAX_GRID_RESULTS_ON_PAGE
				Dim t As Integer = 0
				Dim counter As Integer = 0
				Dim pic As UserPicture
				endIndex = Math.Min(endIndex, Convert.ToInt32(usersFoundCount))
				t = (endIndex - startIndex)
				dgPager.dgListing.StartIndex = startIndex + 1
				dgPager.dgListing.EndIndex = endIndex
				dgPagerUp.dgListing.StartIndex = startIndex + 1
				dgPagerUp.dgListing.EndIndex = endIndex

				Dim dr As DataRow

				dt.Columns.Add(New DataColumn("UserID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("Name", GetType(String)))
				dt.Columns.Add(New DataColumn("URL", GetType(String)))
				dt.Columns.Add(New DataColumn("UserImg", GetType(String)))
				dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))

				dt.Columns.Add(New DataColumn("Gender", GetType(String)))
				dt.Columns.Add(New DataColumn("MStatus", GetType(String)))
				dt.Columns.Add(New DataColumn("MeetPeopleFor", GetType(String)))
				dt.Columns.Add(New DataColumn("Age", GetType(Integer)))
				dt.Columns.Add(New DataColumn("Location", GetType(String)))
				dt.Columns.Add(New DataColumn("ProfileUpdated", GetType(String)))

				dt.Columns.Add(New DataColumn("AddToFavLink", GetType(String)))
				dt.Columns.Add(New DataColumn("SendMessageLink", GetType(String)))
				dt.Columns.Add(New DataColumn("ForwardToFriendLink", GetType(String)))
				dt.Columns.Add(New DataColumn("AddCommentsLink", GetType(String)))
				dt.Columns.Add(New DataColumn("RecommendLink", GetType(String)))
				dt.Columns.Add(New DataColumn("BlockUserLink", GetType(String)))
				dt.Columns.Add(New DataColumn("AddToFriendsLink", GetType(String)))
				dt.Columns.Add(New DataColumn("NameFull", GetType(String)))

				' List search results
				Dim curUserUpdateDate As DateTime
				Dim age As TimeSpan
				Dim ageYears As Double

				Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

				For Each result As User In usersFound
					If counter >= startIndex AndAlso counter < endIndex Then
						dr = dt.NewRow()
						curUserUpdateDate = result.EditDate
						dr(0) = result.ID
						dr(1) = getBeginning(HttpUtility.HtmlEncode(result.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
						dr(18) = result.Name
						dr(2) = "../" + Constants.MEMBERS + "?id=" + result.ID
						pic = result.DefaultPicture
						If pic <> Nothing Then
							dr(3) = Pictures.GetPictureSrcFullURL(pic, True, "../")
						Else
							dr(3) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If
						If LoggedUsersList.Contains(result.ID) Then
							dr(4) = "../images/online_status.gif"
						Else
							dr(4) = "../images/spacer.gif"
						End If

						If result.Gender > 0 Then
							dr(5) = Constants.ARR_GENDER(result.Gender)
						Else
							dr(5) = ""
						End If

						If result.MaritalStatus <> -1 Then
							dr(6) = Constants.ARR_STATUS(result.MaritalStatus)
						Else
							dr(6) = ""
						End If

						dr(7) = ""
						Dim tmp As String = ""
						If result.MatesSR Then
							If tmp <> "" Then
								tmp += ", "
							End If
							tmp += "Serious Relationship"
						End If
						If result.MatesPen Then
							If tmp <> "" Then
								tmp += ", "
							End If
							tmp += "Penpals"
						End If
						If result.MatesFriends Then
							If tmp <> "" Then
								tmp += ", "
							End If
							tmp += "Friends"
						End If
						If result.MatesDating Then
							If tmp <> "" Then
								tmp += ", "
							End If
							tmp += "Dating"
						End If
						If result.MatesAP Then
							If tmp <> "" Then
								tmp += ", "
							End If
							tmp += "Activity Partners"
						End If
						If result.MatesJHO Then
							If tmp <> "" Then
								tmp += ", "
							End If
							tmp += "Just hanging out"
						End If
						dr(7) = tmp
						age = DateTime.Today - result.BirthDay
						ageYears = (age.Days / 365)
						dr(8) = ageYears
						dr(9) += result.City
						If result.City <> Nothing AndAlso result.City <> "" Then
							dr(9) += ", "
						End If

						If result.CountryID <> -1 Then
							dr(9) += Constants.COUNTRIES.GetCountryByID(result.CountryID).Name
						End If
						dr(10) = Constants.ARR_MONTHS(curUserUpdateDate.Month - 1) + " " + curUserUpdateDate.Day + ", " + curUserUpdateDate.Year + " " + curUserUpdateDate.ToShortTimeString()

						'links

						dr(11) = "../" + Constants.ADD_TO_FAVORITES + "?id=" + result.ID
						dr(12) = "../" + Constants.SEND_MESSAGE + "?uid=" + result.ID
						dr(13) = "../" + Constants.SEND_MESSAGE + "?" + Constants.FORWARD_A_FRIEND_PARAM + "=" + result.ID + "&action=" + Constants.MESSAGE_FORWARD_A_FRIEND_ACTION
						dr(14) = "../" + Constants.FRIENDS_ADD_COMMENT + "?usr=" + result.ID
						dr(15) = "../" + Constants.FRIENDS_LIST + "?" + Constants.RECOMMENDED_USER_PARAM + "=" + result.ID
						dr(16) = "../" + Constants.BLOCK_USER + "?id=" + result.ID
						dr(17) = "../" + Constants.ADD_FRIEND_CONFIRMATION_PAGE + "?id=" + result.ID + "&action=" + Constants.NEW_FRIENDSHIP

						dt.Rows.Add(dr)
					End If
					System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
				Next
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid()
			'init datagrid with database data ordered by "orederBy" param
			dgResults.DataSource = CreateDataSource()
			dgResults.DataBind()
		End Sub

		Private Sub dgResults_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim cmd As String = e.CommandName

			If cmd.Equals("MemberRedirect") Then
				Response.Redirect(Constants.MEMBERS + "?id=" + e.CommandArgument)
			End If
		End Sub

		Private Sub dlResults_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
			Dim cmd As String = e.CommandName

			If cmd.Equals("MemberRedirect") Then
				Response.Redirect(Constants.MEMBERS + "?id=" + e.CommandArgument)
			End If
		End Sub

		Private Sub InitAgeDropdown()
			' init age
			Dim i As Integer = Constants.MIN_AGE
			While i <= Constants.MAX_AGE
				ddlminAge.Items.Add(i.ToString())
				ddlminAge.Items.FindByText(i.ToString()).Value = i.ToString()

				ddlmaxAge.Items.Add(i.ToString())
				ddlmaxAge.Items.FindByText(i.ToString()).Value = i.ToString()
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
			ddlmaxAge.ClearSelection()
			ddlmaxAge.Items.FindByText(Constants.BROWSE_MAX_AGE_SELECTED.ToString()).Selected = True
		End Sub

		Private Sub InitCountriesDropdown()
			' init countries
			Dim countriesCount As Integer = Constants.COUNTRIES.List.Count
			Dim tempCountry As Countries.Country

			ddlCountry.Items.Add("-- Select Country --")
			ddlCountry.Items.FindByText("-- Select Country --").Value = "-1"

			Dim i As Integer = 0
			While i < countriesCount
				tempCountry = DirectCast(Constants.COUNTRIES.List(i), Countries.Country)
				ddlCountry.Items.Add(tempCountry.Name)
				ddlCountry.Items.FindByText(tempCountry.Name).Value = tempCountry.ID.ToString()
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
		End Sub

		Private Sub BindPagerControl(ByVal count As Double)
			Dim usr As User = Me.LoggedUser

			If count <> 0 Then
				dgPager.RedirectPage = Constants.BROWSE
				dgPagerUp.RedirectPage = Constants.BROWSE

				If (usr <> Nothing AndAlso Request.Cookies(Constants.cookieBrowse_ViewDetails) <> Nothing AndAlso Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_ViewDetails).Value) = False) OrElse (usr = Nothing AndAlso Session(Constants.ssnBrowse_ViewDetails) <> Nothing AndAlso Convert.ToBoolean(Session(Constants.ssnBrowse_ViewDetails)) = False) Then
					dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(count / Constants.MAX_LIST_RESULTS_ON_PAGE))
					dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(count / Constants.MAX_LIST_RESULTS_ON_PAGE))
				Else
					dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(count / Constants.MAX_GRID_RESULTS_ON_PAGE))
					dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(count / Constants.MAX_GRID_RESULTS_ON_PAGE))
				End If
				dgPager.Action = "-1"
				dgPagerUp.Action = "-1"

				' initialize listing literals
				dgPager.dgListing.TotalCount = Convert.ToInt32(count)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(count)
				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					ViewState("StartIndex") = 0
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					If (usr <> Nothing AndAlso Request.Cookies(Constants.cookieBrowse_ViewDetails) <> Nothing AndAlso Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_ViewDetails).Value) = False) OrElse (usr = Nothing AndAlso Session(Constants.ssnBrowse_ViewDetails) <> Nothing AndAlso Convert.ToBoolean(Session(Constants.ssnBrowse_ViewDetails)) = False) Then
						ViewState("StartIndex") = (dgPager.CurrentPageNumber - 1) * Constants.MAX_LIST_RESULTS_ON_PAGE
					Else
						ViewState("StartIndex") = (dgPager.CurrentPageNumber - 1) * Constants.MAX_GRID_RESULTS_ON_PAGE
					End If
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
			Else
				lblNoResults.Visible = True
				dgPager.Visible = False
				dgPagerUp.Visible = False
				dgResults.Visible = False
				dlResults.Visible = False
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgResults.ItemCommand, AddressOf Me.dgResults_ItemCommand
			AddHandler Me.dlResults.ItemCommand, AddressOf Me.dlResults_ItemCommand

		End Sub
#End Region

		Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim filter As New UserFilter()
			Dim usr As User = Me.LoggedUser
			Dim searchString As String = "<br><b>Search results for</b><br> "

			' set photo filter
			If Request.Params(chkOnlyPeople.UniqueID) = "on" Then
				filter.hasPhoto = True
			End If

			' set result view
			If Convert.ToInt32(Request.Params(rbJustPhotos.NamingContainer.UniqueID + ":" + rbJustPhotos.GroupName)) = Constants.JUST_PHOTOS Then
				If usr <> Nothing Then
					Response.Cookies(Constants.cookieBrowse_ViewDetails).Value = "false"
				Else
					Session(Constants.ssnBrowse_ViewDetails) = False
				End If

				dgResults.Visible = False
				rbJustPhotos.Checked = True
				If usersFoundCount <> 0 Then
					dlResults.Visible = True
					BindList()
				End If
			End If
			If Convert.ToInt32(Request.Params(rbJustPhotos.NamingContainer.UniqueID + ":" + rbJustPhotos.GroupName)) = Constants.PHOTOS_AND_INFO Then
				If usr <> Nothing Then
					Response.Cookies(Constants.cookieBrowse_ViewDetails).Value = "true"
				Else
					Session(Constants.ssnBrowse_ViewDetails) = True
				End If

				dlResults.Visible = False
				rbPhotosAndInfo.Checked = True
				If usersFoundCount <> 0 Then
					dgResults.Visible = True
					BindGrid()
				End If
			End If

			' set gender filter

			searchString += "People who are: "
			Dim fgender As Integer = Convert.ToInt32(Request.Params(rbAll.NamingContainer.UniqueID + ":" + rbAll.GroupName))
			If fgender <> -1 Then
				filter.gender = DirectCast(fgender, UserFilter.Gender)
			Else
				searchString += "All"
			End If

			Dim tmp As String = String.Empty
			Select Case filter.gender
				Case UserFilter.Gender.ufgMale
					If tmp <> String.Empty Then
						tmp += ", "
					End If
					tmp += "Men"
					'searchString += "Men, ";
					Exit Select
				Case UserFilter.Gender.ufgFemale
					If tmp <> String.Empty Then
						tmp += ", "
					End If
					tmp += "Women"
					'searchString += "Women, ";
					Exit Select
			End Select
			searchString += tmp

			searchString += "<br>"
			searchString += "Interested in meeting people for: "

			' set interested in meeting people for / activity preference
			If Request.Params(chkMeetPeopleAll.UniqueID) = "on" Then
				searchString += "All "
				If usr <> Nothing Then
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isMeetPeopleAllChecked) = "true"
				Else
					Session(Constants.ssnIsMeetPeopleAllChecked) = True
				End If
			Else
				If usr <> Nothing Then
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isMeetPeopleAllChecked) = "false"
				Else
					Session(Constants.ssnIsMeetPeopleAllChecked) = False
				End If

				tmp = String.Empty
				If Request.Params(chkMeetPeopleDating.UniqueID) = "on" Then
					filter.activityPreference.dating = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Dating, ";
					tmp += "Dating"
				End If
				If Request.Params(chkMeetPeopleSR.UniqueID) = "on" Then
					filter.activityPreference.relationship = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Serious Relationship, ";
					tmp += "Serious Relationship"
				End If
				If Request.Params(chkMeetPeopleFriends.UniqueID) = "on" Then
					filter.activityPreference.friends = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Friends, ";
					tmp += "Friends"
				End If
				If Request.Params(chkMeetPeopleAP.UniqueID) = "on" Then
					filter.activityPreference.partners = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Activity Partners, ";
					tmp += "Activity Partners"
				End If
				If Request.Params(chkMeetPeoplePenpals.UniqueID) = "on" Then
					filter.activityPreference.penpals = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Penpals, ";
					tmp += "Penpals"
				End If
				If Request.Params(chkMeetPeopleHangingOut.UniqueID) = "on" Then
					filter.activityPreference.hangout = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Just hanging out, ";
					tmp += "Just hanging out"
				End If
				searchString += tmp
			End If

			searchString += "<br>"
			searchString += "Age: From " + Request.Params(ddlminAge.UniqueID)
			searchString += " To " + Request.Params(ddlmaxAge.UniqueID)

			' set min and max age

			filter.ageFrom = Convert.ToInt32(Request.Params(ddlminAge.UniqueID))
			filter.ageTo = Convert.ToInt32(Request.Params(ddlmaxAge.UniqueID))

			searchString += "<br>"
			searchString += "Status: "

			' set status

			If Request.Params(chkStatusAny.UniqueID) = "on" Then
				searchString += "Any "
				If usr <> Nothing Then
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isStatusAnyChecked) = "true"
				Else
					Session(Constants.ssnIsStatusAnyChecked) = True
				End If
			Else
				If usr <> Nothing Then
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isStatusAnyChecked) = "false"
				Else
					Session(Constants.ssnIsStatusAnyChecked) = False
				End If

				tmp = String.Empty
				If Request.Params(chkStatusSingle.UniqueID) = "on" Then
					filter.status.[single] = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Single, ";
					tmp += "Single"
				End If
				If Request.Params(chkStatusInRelationship.UniqueID) = "on" Then
					filter.status.relationship = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "In Relationship, ";
					tmp += "In Relationship"
				End If
				If Request.Params(chkStatusOpenRelationship.UniqueID) = "on" Then
					filter.status.openRelationship = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Open Relationship,  ";
					tmp += "Open Relationship"
				End If
				If Request.Params(chkStatusMarried.UniqueID) = "on" Then
					filter.status.married = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Married, ";
					tmp += "Married"
				End If
				If Request.Params(chkStatusOpenMerriage.UniqueID) = "on" Then
					filter.status.openMarriage = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Open Marriage, ";
					tmp += "Open Marriage"
				End If
				If Request.Params(chkStatusUnknown.UniqueID) = "on" Then
					filter.status.unspecified = True
					If tmp <> String.Empty Then
						tmp += ", "
					End If
						'searchString += "Unknown status, ";
					tmp += "Unknown status"
				End If
				searchString += tmp
			End If

			' set country
			'int countyID = Convert.ToInt32(ddlCountry.SelectedValue);
			Dim countyID As Integer = Convert.ToInt32(Request.Params(ddlCountry.UniqueID))
			If countyID > -1 Then
				filter.countryID = countyID
				searchString += "<br>"
				searchString += "Country: " + Constants.COUNTRIES.GetCountryByID(countyID).Name
			End If

			'  city / town
			Dim city As String = Request.Params(txtCity.UniqueID)
			If city <> Nothing AndAlso city <> "" Then
				filter.city = city
				searchString += "<br>"
				searchString += "City/Town: " + city
			End If

			searchString += "<br>"

			' set Orderby option

			Dim fOrderBy As Integer = Convert.ToInt32(Request.Params(rbListByRecentlyUpdated.NamingContainer.UniqueID + ":" + rbListByRecentlyUpdated.GroupName))
			filter.orderBy = DirectCast(fOrderBy, UserFilter.OrderBy)

			If usr <> Nothing Then
				Response.Cookies(Constants.cookieBrowse_Criteria).Value = Server.UrlEncode(searchString)
				initBrowseCookies(filter)
			Else
				Session(Constants.ssnBrowse_Criteria) = Server.UrlEncode(searchString)
				ssn_InitBrowseSession(filter)
			End If

			Response.Redirect(Constants.BROWSE)
		End Sub

		Private Sub initBrowseCookies(ByVal usr As User)
			Dim f As New UserFilter()

			Response.Cookies(Constants.cookieBrowse_Filter).Expires = DateTime.MaxValue
			Response.Cookies(Constants.cookieBrowse_Criteria).Expires = DateTime.MaxValue
			Response.Cookies(Constants.cookieBrowse_ViewDetails).Expires = DateTime.MaxValue

			'Response.Cookies[Constants.cookieBrowse_Filter][Constants.cookieBrowse_AgeFrom] = f.ageFrom.ToString();
			'Response.Cookies[Constants.cookieBrowse_Filter][Constants.cookieBrowse_AgeTo] = f.ageTo.ToString();

			' change requested in bug 380
			Dim ageFrom As Double = Constants.MIN_AGE
			Dim ageTo As Double = Constants.MAX_AGE

			Dim age As TimeSpan = DateTime.Today - usr.BirthDay
			Dim ageYears As Double = (age.Days / 365)
			ageFrom = Math.Max(ageYears - 5, Constants.MIN_AGE)
			ageTo = Math.Min(ageYears + 5, Constants.MAX_AGE)

			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_AgeFrom) = ageFrom.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_AgeTo) = ageTo.ToString()

			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_HasPhoto) = f.hasPhoto.ToString()
			Select Case f.gender
				Case UserFilter.Gender.ufgAny
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender) = Constants.GENDER_ANY.ToString()
					Exit Select
				Case UserFilter.Gender.ufgFemale
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender) = Constants.GENDER_FEMALE.ToString()
					Exit Select
				Case UserFilter.Gender.ufgMale
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender) = Constants.GENDER_MALE.ToString()
					Exit Select
				Case UserFilter.Gender.ufgUnspecified
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender) = Constants.GENDER_UNSPECIFIED.ToString()
					Exit Select
			End Select

			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APDating) = f.activityPreference.dating.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APRelationship) = f.activityPreference.relationship.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APFriends) = f.activityPreference.friends.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APPartners) = f.activityPreference.partners.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APPenpals) = f.activityPreference.penpals.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APHangout) = f.activityPreference.hangout.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusSingle) = f.status.[single].ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusInRelationship) = f.status.relationship.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusOpenRelationship) = f.status.openRelationship.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusMarried) = f.status.married.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusOpenMarriage) = f.status.openMarriage.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusUnknown) = f.status.unspecified.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Country) = f.countryID.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_City) = f.city
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_OrderBy) = f.orderBy.ToString()

			Response.Cookies(Constants.cookieBrowse_Criteria).Value = ""
			Response.Cookies(Constants.cookieBrowse_ViewDetails).Value = "true"
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isMeetPeopleAllChecked) = "false"
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_isStatusAnyChecked) = "false"

			f.ageFrom = Convert.ToInt32(ageFrom)
			f.ageTo = Convert.ToInt32(ageTo)

			' init gender preference
			Select Case usr.GenderPreference
				Case Constants.GENDER_PREFERENCE_MALE
					f.gender = UserFilter.Gender.ufgMale
					Exit Select
				Case Constants.GENDER_PREFERENCE_FEMALE
					f.gender = UserFilter.Gender.ufgFemale
					Exit Select
			End Select

			' init Meeting People for

			If usr.MatesDating Then
				f.activityPreference.dating = True
			End If
			If usr.MatesSR Then
				f.activityPreference.relationship = True
			End If
			If usr.MatesFriends Then
				f.activityPreference.friends = True
			End If
			If usr.MatesAP Then
				f.activityPreference.partners = True
			End If
			If usr.MatesPen Then
				f.activityPreference.penpals = True
			End If
			If usr.MatesJHO Then
				f.activityPreference.hangout = True
			End If

			' init country
			If usr.CountryID > 0 Then
				f.countryID = usr.CountryID
			End If

			' init city/town
			If usr.City <> Nothing AndAlso usr.City <> "" Then
				f.city = usr.City
			End If

			' init status
			If usr.MaritalStatus >= 0 Then
				Select Case usr.MaritalStatus
					Case Constants.MSTATUS_SINGLE
						f.status.[single] = True
						Exit Select
					Case Constants.MSTATUS_IN_RELATIONSHIP
						f.status.relationship = True
						Exit Select
					Case Constants.MSTATUS_OPEN_RELATIONSHIP
						f.status.openRelationship = True
						Exit Select
					Case Constants.MSTATUS_OPEN_MARRIAGE
						f.status.openMarriage = True
						Exit Select
					Case Constants.MSTATUS_MARRIED
						f.status.married = True
						Exit Select
					Case Constants.MSTATUS_UNKNOWN
						f.status.unspecified = True
						Exit Select
				End Select
			End If

			initBrowseCookies(f)
		End Sub

		Private Sub initBrowseCookies(ByVal f As UserFilter)

			Response.Cookies(Constants.cookieBrowse_Filter).Expires = DateTime.MaxValue

			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_AgeFrom) = f.ageFrom.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_AgeTo) = f.ageTo.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_HasPhoto) = f.hasPhoto.ToString()
			Select Case f.gender
				Case UserFilter.Gender.ufgAny
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender) = Constants.GENDER_ANY.ToString()
					Exit Select
				Case UserFilter.Gender.ufgFemale
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender) = Constants.GENDER_FEMALE.ToString()
					Exit Select
				Case UserFilter.Gender.ufgMale
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender) = Constants.GENDER_MALE.ToString()
					Exit Select
				Case UserFilter.Gender.ufgUnspecified
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender) = Constants.GENDER_UNSPECIFIED.ToString()
					Exit Select
			End Select

			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APDating) = f.activityPreference.dating.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APRelationship) = f.activityPreference.relationship.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APFriends) = f.activityPreference.friends.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APPartners) = f.activityPreference.partners.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APPenpals) = f.activityPreference.penpals.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APHangout) = f.activityPreference.hangout.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusSingle) = f.status.[single].ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusInRelationship) = f.status.relationship.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusOpenRelationship) = f.status.openRelationship.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusMarried) = f.status.married.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusOpenMarriage) = f.status.openMarriage.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusUnknown) = f.status.unspecified.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Country) = f.countryID.ToString()
			Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_City) = f.city

			Select Case f.orderBy
				Case UserFilter.OrderBy.defaultOption
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_OrderBy) = Constants.ORDERBY_DEFAULT.ToString()
					Exit Select
				Case UserFilter.OrderBy.lastLogin
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_OrderBy) = Constants.ORDERBY_LAST_LOGIN.ToString()
					Exit Select
				Case UserFilter.OrderBy.newUsers
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_OrderBy) = Constants.ORDERBY_NEW_USERS.ToString()
					Exit Select
				Case UserFilter.OrderBy.recentlyUpdated
					Response.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_OrderBy) = Constants.ORDERBY_RECENTLY_UPDATED.ToString()
					Exit Select
			End Select

			'bool isMeetPeopleAllChecked = 
'			!f.activityPreference.dating &&
'				!f.activityPreference.relationship &&
'				!f.activityPreference.friends &&
'				!f.activityPreference.partners &&
'				!f.activityPreference.penpals &&
'				!f.activityPreference.hangout;
'
'			bool isStatusAnyChecked = 
'				!f.status.single &&
'				!f.status.relationship &&
'				!f.status.openRelationship &&
'				!f.status.married &&
'				!f.status.openMarriage &&
'				!f.status.unspecified;
'
'
'			Response.Cookies[Constants.cookieBrowse_Filter][Constants.cookieBrowse_isMeetPeopleAllChecked] = isMeetPeopleAllChecked.ToString();
'			Response.Cookies[Constants.cookieBrowse_Filter][Constants.cookieBrowse_isStatusAnyChecked] = isStatusAnyChecked.ToString();


		End Sub

		Private Function initUserFilter() As UserFilter
			Dim f As New UserFilter()
			Dim curGender As Integer, curOrderBy As Integer

			f.ageFrom = Convert.ToInt32(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_AgeFrom))
			f.ageTo = Convert.ToInt32(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_AgeTo))
			f.hasPhoto = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_HasPhoto))
			curGender = Convert.ToInt32(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Gender))
			Select Case curGender
				Case Constants.GENDER_ANY
					f.gender = UserFilter.Gender.ufgAny
					Exit Select
				Case Constants.GENDER_FEMALE
					f.gender = UserFilter.Gender.ufgFemale
					Exit Select
				Case Constants.GENDER_MALE
					f.gender = UserFilter.Gender.ufgMale
					Exit Select
				Case Constants.GENDER_UNSPECIFIED
					f.gender = UserFilter.Gender.ufgUnspecified
					Exit Select
			End Select

			f.activityPreference.dating = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APDating))
			f.activityPreference.relationship = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APRelationship))
			f.activityPreference.friends = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APFriends))
			f.activityPreference.partners = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APPartners))
			f.activityPreference.penpals = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APPenpals))
			f.activityPreference.hangout = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_APHangout))
			f.status.[single] = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusSingle))
			f.status.relationship = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusInRelationship))
			f.status.openRelationship = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusOpenRelationship))
			f.status.married = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusMarried))
			f.status.openMarriage = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusOpenMarriage))
			f.status.unspecified = Convert.ToBoolean(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_StatusUnknown))
			f.countryID = Convert.ToInt32(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_Country))
			f.city = Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_City)
			curOrderBy = Convert.ToInt32(Request.Cookies(Constants.cookieBrowse_Filter)(Constants.cookieBrowse_OrderBy))
			Select Case curOrderBy
				Case Constants.ORDERBY_DEFAULT
					f.orderBy = UserFilter.OrderBy.defaultOption
					Exit Select
				Case Constants.ORDERBY_LAST_LOGIN
					f.orderBy = UserFilter.OrderBy.lastLogin
					Exit Select
				Case Constants.ORDERBY_NEW_USERS
					f.orderBy = UserFilter.OrderBy.newUsers
					Exit Select
				Case Constants.ORDERBY_RECENTLY_UPDATED
					f.orderBy = UserFilter.OrderBy.recentlyUpdated
					Exit Select
			End Select

			Return f
		End Function

		' this function is used for browsing when the user is not logged in
		Private Sub ssn_InitBrowseSession()
			Dim f As New UserFilter()

			' init with default values
			f.hasPhoto = True
			f.gender = UserFilter.Gender.ufgFemale
			f.countryID = Countries.UKID
			f.status.[single] = True
			f.orderBy = UserFilter.OrderBy.newUsers

			Session(Constants.ssnBrowseFilter) = f
			Session(Constants.ssnBrowse_Criteria) = ""
			Session(Constants.ssnBrowse_ViewDetails) = False
			Session(Constants.ssnIsMeetPeopleAllChecked) = True
			Session(Constants.ssnIsStatusAnyChecked) = False
			ssn_InitBrowseSession(f)
		End Sub

		' this function is used for browsing when the user is not logged in
		Private Sub ssn_InitBrowseSession(ByVal f As UserFilter)
			Session(Constants.ssnBrowseFilter) = f
		End Sub

		' this function is used for browsing when the user is not logged in
		Private Function ssn_InitUserFilter() As UserFilter
			Dim f As New UserFilter()
			f = DirectCast(Session(Constants.ssnBrowseFilter), UserFilter)
			Return f
		End Function

		Private Function IsSearchCriteriaSelected(ByVal filter As UserFilter) As Boolean
			Dim bIsSetMeetPeopleFilter As Boolean = True
			Dim bIsSetStatusFilter As Boolean = True

			If (chkMeetPeopleAll.Checked = True) OrElse ((chkMeetPeopleAll.Checked = False) AndAlso (chkMeetPeopleAP.Checked = False) AndAlso (chkMeetPeopleDating.Checked = False) AndAlso (chkMeetPeopleFriends.Checked = False) AndAlso (chkMeetPeopleHangingOut.Checked = False) AndAlso (chkMeetPeoplePenpals.Checked = False) AndAlso (chkMeetPeopleSR.Checked = False)) Then
				bIsSetMeetPeopleFilter = False
			End If

			If (chkStatusAny.Checked = True) OrElse ((chkStatusAny.Checked = False) AndAlso (chkStatusInRelationship.Checked = False) AndAlso (chkStatusMarried.Checked = False) AndAlso (chkStatusOpenMerriage.Checked = False) AndAlso (chkStatusOpenRelationship.Checked = False) AndAlso (chkStatusSingle.Checked = False) AndAlso (chkStatusUnknown.Checked = False)) Then
				bIsSetStatusFilter = False
			End If

			If (rbMen.Checked = False) AndAlso (rbWomen.Checked = False) AndAlso (bIsSetMeetPeopleFilter = False) AndAlso (filter.countryID < 0) AndAlso (txtCity.Text = "") AndAlso (bIsSetStatusFilter = False) AndAlso ((Convert.ToInt32(ddlminAge.SelectedItem.Value) = Constants.MIN_AGE) AndAlso (Convert.ToInt32(ddlmaxAge.SelectedItem.Value) = Constants.MAX_AGE)) Then
				Return False
			End If


			Return True
		End Function

	End Class
End Namespace






